package fpt.hungnp6.boxlauncher;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class App extends Activity{
	PackageManager myPackageManager;
	
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app);
		
		myPackageManager = getPackageManager();
		
		Intent intent = new Intent(Intent.ACTION_MAIN,null);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		
		List<ResolveInfo> appIntentList = getPackageManager().queryIntentActivities(intent, 0);
		
		GridView gridView = (GridView) findViewById(R.id.gv_app);
		gridView.setAdapter(new MyAdapter(this, appIntentList));
		gridView.setOnItemClickListener( myOnItemClickListener);
		
	}
	
	OnItemClickListener myOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			ResolveInfo cleckedResolveInfo   = (ResolveInfo) parent.getItemAtPosition(position);
			ActivityInfo clickedActivityInfo = cleckedResolveInfo.activityInfo;
			
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_LAUNCHER);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
			startActivity(intent);
		}
	};

}
